package com.hd.trans.framework.varyview;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hd.trans.R;
import com.hd.trans.ui.base.AppContextProvider;
import com.hd.trans.ui.base.TransInit;


public class VaryViewHelperController {


	private IVaryViewHelper helper;

	public VaryViewHelperController(View view) {
		this(new VaryViewHelper(view));
	}

	public VaryViewHelperController(IVaryViewHelper helper) {
		super();
		this.helper = helper;
	}



	public void showCommonMsgView(String msg, int iconId, final View.OnClickListener onClickListener) {
		View layout = helper.inflate(R.layout.phi_comm_error);
		TextView textView = (TextView) layout.findViewById(R.id.tv_tips);
		String defaultMsg = AppContextProvider.getContext().getResources().getString(R.string.common_error_msg);
		int defaultId = R.mipmap.ic_common_error;
		textView.setText(!TextUtils.isEmpty(msg) ? msg : defaultMsg);
		ImageView imageView = (ImageView) layout.findViewById(R.id.icon);
		imageView.setImageResource(iconId != -1 ? iconId : defaultId);

		if (null != onClickListener ) {
			layout.setOnClickListener(onClickListener);
		}
		helper.showLayout(layout);
	}


	public void showCustomMsgView(int layoutId, int msgViewId , String msg, int clickViewId, final View.OnClickListener listener  ) {
		try{
			View layout = helper.inflate(layoutId);
			TextView textView = layout.findViewById(msgViewId);
			View clickView = layout.findViewById(clickViewId);
			if( textView!=null)
				textView.setText(msg);
			if (null != listener && clickView!=null) {
				clickView.setOnClickListener( listener );
			}
			helper.showLayout(layout);
		}catch (Exception e){
			e.printStackTrace();
		}
	}


	public void showLoading(String msg) {
		View layout = helper.inflate(R.layout.phi_comm_loading);
		if (!TextUtils.isEmpty(msg)) {
			TextView textView = (TextView) layout
					.findViewById(R.id.loading_msg);
			textView.setText(msg);
		}
		helper.showLayout(layout);
	}

	public void showLoading(int resId) {
		if( getContext()!=null ){
			String msg = getContext().getString(resId);
			View layout = helper.inflate(R.layout.phi_comm_loading);
			if (!TextUtils.isEmpty(msg)) {
				TextView textView = (TextView) layout
						.findViewById(R.id.loading_msg);
				textView.setText(msg);
			}
			helper.showLayout(layout);
		}
	}

	public void restore() {
		helper.restoreView();
	}

	public Context getContext(){
		if(helper!=null)
			return  helper.getContext();
		return null;
	}
}
